<?php
namespace app\admin\controller;

use app\AdminCollection;
use app\model\Scrollcolumnmodel;
use app\model\Scrollinfomodel;
use My\Tool;
use My\Validate;
use think\Db;

class Scroll extends AdminCollection
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $input = input('');
        $page = isset($input['page']) ? $input['page'] : 1;
        $where = array();
        $list = Scrollinfomodel::pageService($where);
        $orderkey = pagesize * ($page - 1);
        $_list = array();
        foreach ($list as $key => $val) {
            $_t = $val;
            $_t['orderkey'] = $orderkey += 1;
            $_t['inputtime'] = $val['inputtime'] != 0 ? date('Y-m-d H:i:s', $val['inputtime']) : '0000-00-00 00:00:00';
            $_t['deleteurl'] = url('scroll/delete', array('id' => $val['id']));
            $_t['updateurl'] = url('scroll/index', array('id' => $val['id']));
            $_list[] = $_t;
        }
        $pages = $list->render();
        $id = isset($input['id']) ? $input['id'] : 0;
        $findService = [];
        $findService['id'] = '';
        $findService['title'] = '';
        $findService['url'] = 'http://';
        $findService['list_order'] = 0;
        $findService['thumb'] = config('path.images') . 'upload_default.png';
        if ($id) {
            $findService = Scrollinfomodel::findService(['id' => $id]);
            $this->assign('scroll_column_id', $findService['scroll_column_id']);
            $this->assign('addurl', url('scroll/update'));
        } else {
            $this->assign('scroll_column_id', $input['scroll_column_id']);
            $this->assign('addurl', url('scroll/add'));
        }
        if ($findService['thumb'] == "") $findService['thumb'] = config('path.images') . 'upload_default.png';
        $this->assign('list', $_list);
        $this->assign('findService', $findService);
        $this->assign('pages', $pages);
        return $this->fetch();
    }

    /**
     * 更新类型
     * @throws \Exception
     */
    public function update()
    {
        try {
            $input = input('');
            $data = $input['info'];
            $where = $input['where'];
            $title = $data['title'];
            $url = $data['url'];
            $thumb = $data['thumb'];
            $list_order = $data['list_order'];
            if (Validate::checkNull($title)) throw new \Exception("请输入标题名称");
            if (Validate::checkNull($url)) throw new \Exception("请输入URL");
            if (Validate::checkNull($thumb)) throw new \Exception("请选择上传图片");
            $return = Scrollinfomodel::updateServer($where, $data);
            $this->returnSuccess("提交数据成功", url('scroll/index', array('scroll_column_id' => $data['scroll_column_id'])));
        } catch (\Exception $e) {
            $this->returnError($e->getMessage());
        }
    }

    /**
     * 删除信息
     */
    public function delete()
    {
        try {
            $id = input('id');
            if (Validate::checkNull($id)) throw new \Exception("删除失败");
            $photosfind = Scrollinfomodel::findService(['id' => $id]);
            if (Validate::checkEmpty($photosfind)) throw new \Exception("删除失败");
            $return = Scrollinfomodel::deleteService(['id' => $id]);
            if ($return) {
                $this->returnSuccess("删除成功");
            } else {
                throw new \Exception("删除失败");
            }
        } catch (\Exception $e) {
            $this->returnError($e->getMessage());
        }
    }

    /**
     * 添加信息
     */
    public function add()
    {
        try {
            $input = input('');
            $data = $input['info'];
            $title = $data['title'];
            $url = $data['url'];
            $thumb = $data['thumb'];
            $list_order = $data['list_order'];
            if (Validate::checkNull($title)) throw new \Exception("请输入标题名称");
            if (Validate::checkNull($url)) throw new \Exception("请输入URL");
            if (Validate::checkNull($thumb)) throw new \Exception("请选择上传图片");
            $data['admin_id'] = $this->getAdmin();
            $data['inputtime'] = time();
            $return = Scrollinfomodel::addService($data);
            if ($return) {
                $this->returnSuccess("提交数据成功");
            } else {
                throw new \Exception("提交数据失败");
            }
        } catch (\Exception $e) {
            $this->returnError($e->getMessage());
        }
    }

    /**
     * 栏目管理
     * @return mixed
     */
    public function column()
    {
        $input = input('');
        $page = isset($input['page']) ? $input['page'] : 1;
        $where = array();
        $list = Scrollcolumnmodel::pageService($where);
        $orderkey = pagesize * ($page - 1);
        $_list = array();
        foreach ($list as $key => $val) {
            $_t = $val;
            $_t['orderkey'] = $orderkey += 1;
            $_t['infocount'] = Scrollinfomodel::countService(['scroll_column_id' => $val['scroll_column_id']]);
            $_t['inputtime'] = $val['inputtime'] != 0 ? date('Y-m-d H:i:s', $val['inputtime']) : '0000-00-00 00:00:00';
            $_t['indexurl'] = url('scroll/index', array('scroll_column_id' => $val['scroll_column_id']));
            $_t['deleteurl'] = url('scroll/deletecolumn', array('scroll_column_id' => $val['scroll_column_id']));
            $_t['updateurl'] = url('scroll/updatecolumn', array('scroll_column_id' => $val['scroll_column_id']));
            $_list[] = $_t;
        }
        $pages = $list->render();
        $this->assign('list', $_list);
        $this->assign('pages', $pages);
        return $this->fetch();
    }

    /**
     * 编辑栏目
     * @return mixed
     */
    public function updatecolumn()
    {
        $submit = input('submit');
        if ($submit) {
            try {
                $input = input('');
                $where = $input['where'];
                $data = $input['info'];
                $title = $data['title'];
                if (Validate::checkNull($title)) throw new \Exception("请输入标题名称");
                $findService = Scrollcolumnmodel::findService($where);
                if (Validate::checkEmpty($findService)) throw new \Exception("操作失败");
                if ($title != $findService['title']) {
                    $titlefindservice = Scrollcolumnmodel::findService(['title' => $title]);
                    if ($titlefindservice) throw new \Exception("标题名称已经存在，请重新输入");
                }
                $return = Scrollcolumnmodel::updateServer($where, $data);
                if ($return) {
                    $this->returnSuccess("提交数据成功");
                } else {
                    throw new \Exception("没有数据更新...");
                }
            } catch (\Exception $e) {
                $this->returnError($e->getMessage());
            }
        } else {
            try {
                $scroll_column_id = input('scroll_column_id');
                if (Validate::checkNull($scroll_column_id)) throw new \Exception("操作失败");
                $findService = Scrollcolumnmodel::findService(['scroll_column_id' => $scroll_column_id]);
                if (Validate::checkEmpty($findService)) throw new \Exception("操作失败");
                $this->assign('findService', $findService);
                return $this->fetch();
            } catch (\Exception $e) {
                $this->returnError($e->getMessage());
            }
        }
    }

    /**
     * 删除栏目
     */
    public function deletecolumn()
    {
        try {
            $scroll_column_id = input('scroll_column_id');
            if (Validate::checkNull($scroll_column_id)) throw new \Exception("删除失败");
            $photosfind = Scrollcolumnmodel::findService(['scroll_column_id' => $scroll_column_id]);
            if (Validate::checkEmpty($photosfind)) throw new \Exception("删除失败");
            $return = Scrollcolumnmodel::deleteService(['scroll_column_id' => $scroll_column_id]);
            if ($return) {
                $this->returnSuccess("删除成功");
            } else {
                throw new \Exception("删除失败");
            }
        } catch (\Exception $e) {
            $this->returnError($e->getMessage());
        }
    }

    /**
     * 添加滚动条栏目
     * @return mixed
     */
    public function addcolumn()
    {
        $submit = input('submit');
        if ($submit) {
            try {
                $input = input('');
                $data = $input['info'];
                $title = $data['title'];
                $list_order = $data['list_order'];
                if (Validate::checkNull($title)) throw new \Exception("请输入标题名称");
                $findService = Scrollcolumnmodel::findService(['title' => $title]);
                if ($findService) throw new \Exception("标题名称已经存在，请重新输入");
                $data['title'] = $title;
                $data['list_order'] = $list_order;
                $data['admin_id'] = $this->getAdmin();
                $data['inputtime'] = time();
                $return = Scrollcolumnmodel::addService($data);
                if ($return) {
                    $this->returnSuccess("提交数据成功");
                } else {
                    throw new \Exception("提交数据失败");
                }
            } catch (\Exception $e) {
                $this->returnError($e->getMessage());
            }
        } else {
            return $this->fetch();
        }
    }
}
